import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';

interface Router {
  back: () => void;
  forward: () => void;
  refresh: () => void;
  push: (href: string, options?: { replace?: boolean }) => void;
  replace: (href: string) => void;
  currentPath: string;
  query: URLSearchParams;
}

export function useRouter(): Router {
  const navigate = useNavigate();

  const router = useMemo(() => {
    return {
      back: () => navigate(-1),
      forward: () => navigate(1),
      refresh: () => navigate(0),
      push: (href: string, options?: { replace?: boolean }) => navigate(href, options),
      replace: (href: string) => navigate(href, { replace: true }),
      currentPath: window.location.pathname,
      query: new URLSearchParams(window.location.search),
    };
  }, [navigate]);

  return router;
}
